package com.fowo.api.documentCirculation.sql;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

/**
 * SQL 和参数打包类
 * @author yl_ls
 */
@Getter
@Setter
@Accessors(chain = true)
public class SqlAndArgs {
    private String sql;
    private List<Object> args = new ArrayList<>();
    public SqlAndArgs addArgs(Object... args) {
        this.args.addAll(List.of(args));
        return this;
    }

    public Object[] toArgsArray() {
        return this.args.toArray();
    }

    public String toDebugString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(sql);
        if (!this.args.isEmpty()) {
            stringBuilder.append("\n");
            stringBuilder.append(args.size()).append(" Parameters:");
            for (int i = 0; i < args.size(); i++) {
                stringBuilder.append("\nParameter ").append(i).append(" : ");
                Object arg = args.get(i);
                if (arg == null) {
                    stringBuilder.append("null");
                } else {
                    stringBuilder.append(arg);
                }
            }
        }
        return stringBuilder.toString();
    }
}
